//
//  CXHViewController.m
//  CXHAdSDK-ToBid
//
//  Created by 二跳 on 04/03/2025.
//  Copyright (c) 2025 二跳. All rights reserved.
//

#import "CXHViewController.h"
#import <WindMillSDK/WindMillSDK.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>

@interface CXHViewController ()<WindMillBannerViewDelegate,WindMillIntersititialAdDelegate, WindMillNativeAdsManagerDelegate,WindMillNativeAdViewDelegate>
@property (nonatomic, strong) WindMillIntersititialAd *intersititialAd;
@property (nonatomic, strong) WindMillBannerView *bannerView;
@property (nonatomic, strong) WindMillNativeAdsManager *nativeAdsManager;

@end

@implementation CXHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self loadNativeExpressAd];
    });
}

#pragma mark - load nativeExpress
- (void)loadNativeExpressAd {
    CGFloat width = UIScreen.mainScreen.bounds.size.width;
    CGFloat height = 10;
    WindMillAdRequest *request = [WindMillAdRequest request];
    request.placementId = @"9848564981692282";
//    request.userId = @"your user id";
//    request.options =
    self.nativeAdsManager = [[WindMillNativeAdsManager alloc] initWithRequest:request];
    self.nativeAdsManager.extra = @{@"isExpress":@0};//s2s激励回传自定义参数，可以为nil
    self.nativeAdsManager.adSize = CGSizeMake(width, height);
    self.nativeAdsManager.delegate = self;
    [self.nativeAdsManager loadAdDataWithCount:1];
}

#pragma mark - WindMillNativeAdsManagerDelegate
- (void)nativeAdsManagerSuccessToLoad:(WindMillNativeAdsManager *)adsManager {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), adsManager.placementId);
    NSArray<WindMillNativeAd *> *nativeAdList = [self.nativeAdsManager getAllNativeAds];
    
    if ([[adsManager.extra objectForKey:@"isExpress"] intValue] == 1) {
        WindMillNativeAd *nativeAd = nativeAdList.firstObject;
        WindMillNativeAdView *view = [WindMillNativeAdView new];
        [view refreshData:nativeAd];
        view.backgroundColor = [UIColor redColor];
        view.viewController = self;
        view.delegate = self;
        CGSize size = view.frame.size;
        view.frame= CGRectMake(0, 200, size.width, size.height);
        [self.view addSubview:view];
    } else {
        WindMillNativeAd *nativeAd = nativeAdList.firstObject;
        WindMillNativeAdView *view = [WindMillNativeAdView new];
        [view refreshData:nativeAd];
        
        CGFloat width = [UIScreen mainScreen].bounds.size.width - 16 * 2;
        CGFloat height = width * 9 / 16;
        
        view.frame = CGRectMake(0, 200, UIScreen.mainScreen.bounds.size.width, height + 20);
        
        UIView *adView = [[UIView alloc]initWithFrame:CGRectMake(16, 10, width, height)];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.userInteractionEnabled = YES;
        [imageView cxh_setImageWithURL:[NSURL URLWithString:nativeAd.imageUrlList.firstObject]];
        [adView addSubview:imageView];
        [view addSubview:adView];
        [view setClickableViews:@[adView]];
        
        view.backgroundColor = [UIColor redColor];
        view.viewController = self;
        view.delegate = self;
        [self.view addSubview:view];
        
    }
}

- (void)nativeAdsManager:(WindMillNativeAdsManager *)adsManager didFailWithError:(NSError *)error {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), adsManager.placementId);
}

#pragma mark - WindMillNativeAdViewDelegate
- (void)nativeExpressAdViewRenderSuccess:(WindMillNativeAdView *)nativeExpressAdView {
    NSLog(@"%@", NSStringFromSelector(_cmd));
    //渲染成功后更新约束
    CGSize size = nativeExpressAdView.frame.size;
    nativeExpressAdView.frame = CGRectMake(0, 200, size.width, size.height);
}

- (void)nativeExpressAdViewRenderFail:(WindMillNativeAdView *)nativeExpressAdView error:(NSError *)error {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

/**
 广告曝光回调

 @param nativeAdView WindMillNativeAdView 实例
 */
- (void)nativeAdViewWillExpose:(WindMillNativeAdView *)nativeAdView{
    
}

/**
 广告点击回调

 @param nativeAdView WindMillNativeAdView 实例
 */
- (void)nativeAdViewDidClick:(WindMillNativeAdView *)nativeAdView{
    
}

#pragma mark - load inter
- (void)loadIntersititialAd{
    WindMillAdRequest *request = [WindMillAdRequest request];
//    request.userId = @"your user id";
    request.placementId = @"2183882261822852";
//    request.options = @{@"test_key":@"test_value"};
    self.intersititialAd = [[WindMillIntersititialAd alloc] initWithRequest:request];
    self.intersititialAd.delegate = self;
    [self.intersititialAd loadAdData];
}
#pragma mark - WindMillIntersititialAdDelegate
- (void)intersititialAdDidLoad:(WindMillIntersititialAd *)intersititialAd {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), intersititialAd.placementId);
    [self.intersititialAd showAdFromRootViewController:self options:nil];
}

- (void)intersititialAdDidLoad:(WindMillIntersititialAd *)intersititialAd didFailWithError:(NSError *)error {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), intersititialAd.placementId);
}

- (void)intersititialAdWillVisible:(WindMillIntersititialAd *)intersititialAd {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), intersititialAd.placementId);
}

- (void)intersititialAdDidVisible:(WindMillIntersititialAd *)intersititialAd {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), intersititialAd.placementId);
}

- (void)intersititialAdDidClick:(WindMillIntersititialAd *)intersititialAd {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), intersititialAd.placementId);
}

- (void)intersititialAdDidClickSkip:(WindMillIntersititialAd *)intersititialAd {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), intersititialAd.placementId);
}

- (void)intersititialAdDidClose:(WindMillIntersititialAd *)intersititialAd {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), intersititialAd.placementId);
}

- (void)intersititialAdDidPlayFinish:(WindMillIntersititialAd *)intersititialAd didFailWithError:(NSError *)error {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), intersititialAd.placementId);
}


#pragma mark - load banner

- (void)loadBanner{
    CGFloat width = UIScreen.mainScreen.bounds.size.width;
    CGFloat height = width * 5 / 32;
    WindMillAdRequest *request = [WindMillAdRequest request];
    request.placementId = @"2339387191514005";
    request.options = @{@"adSize":NSStringFromCGSize(CGSizeMake(width, height))};
//    request.userId = @"Your User Id";
    self.bannerView = [[WindMillBannerView alloc] initWithRequest:request expectSize:CGSizeMake(UIScreen.mainScreen.bounds.size.width, height)];
    self.bannerView.frame = CGRectMake(0, 200, UIScreen.mainScreen.bounds.size.width, height);
    self.bannerView.backgroundColor = UIColor.yellowColor;
    self.bannerView.delegate = self;
    self.bannerView.viewController = self;
    [self.view addSubview:self.bannerView];
    [self.bannerView loadAdData];
}

#pragma mark - WindMillBannerViewDelegate
//bannerView自动刷新
- (void)bannerAdViewDidAutoRefresh:(WindMillBannerView *)bannerAdView {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), bannerAdView.placementId);
}
//bannerView自动刷新失败
- (void)bannerView:(WindMillBannerView *)bannerAdView failedToAutoRefreshWithError:(NSError *)error {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), bannerAdView.placementId);
}
//成功加载广告
- (void)bannerAdViewLoadSuccess:(WindMillBannerView *)bannerAdView {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), bannerAdView.placementId);
}
//广告加载失败
- (void)bannerAdViewFailedToLoad:(WindMillBannerView *)bannerAdView error:(NSError *)error {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), bannerAdView.placementId);
}
//广告将要展示
- (void)bannerAdViewWillExpose:(WindMillBannerView *)bannerAdView {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), bannerAdView.placementId);
}
//广告被点击
- (void)bannerAdViewDidClicked:(WindMillBannerView *)bannerAdView {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), bannerAdView.placementId);
}
//当用户由于点击要离开您的应用程序时触发该回调,您的应用程序将移至后台
- (void)bannerAdViewWillLeaveApplication:(WindMillBannerView *)bannerAdView {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), bannerAdView.placementId);
}
//将打开全屏视图。在打开storekit或在应用程序中打开网页时触发
- (void)bannerAdViewWillOpenFullScreen:(WindMillBannerView *)bannerAdView {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), bannerAdView.placementId);
}
//将关闭全屏视图。关闭storekit或关闭应用程序中的网页时发送
- (void)bannerAdViewCloseFullScreen:(WindMillBannerView *)bannerAdView {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), bannerAdView.placementId);
}
//广告视图被移除
- (void)bannerAdViewDidRemoved:(WindMillBannerView *)bannerAdView {
    NSLog(@"%@ -- %@", NSStringFromSelector(_cmd), bannerAdView.placementId);
    [self.bannerView removeFromSuperview];
    self.bannerView.delegate = nil;
    self.bannerView = nil;
}

@end
